home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 335 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c,comp.std.c,comp.lang.c++
  4. Subject: Re: Floating Point arithmetic problem
  5. Date: 15 Feb 1996 02:05:35 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824366374@pegasus.montclair.edu>
  8. References: <c968da6jzm.fsf@damayanti.india.ti.com>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. kuntal@india.ti.com (Kuntal Shah) writes:
  13. >This addition  usually results in  a  few insignificant digits getting
  14. >accumulated in my double variable. For eg.
  15.  
  16. >  f = 12.99  is represented as 12.9900000000000002131628207
  17. >  f = 11.111 is represented as 11.1110000000000006536993169
  18.  
  19. >when the accrued value exceeds 0.0001 which  results in failure of the
  20. >if condition in the  above block of code,  when ideally no such  thing
  21. >should have occurred.
  22.  
  23. >I have a few options to overcome this problem :-
  24.  
  25. >* After each   addition,   covert  'd' to   an  unsigned   long  after
  26. >  multiplying  by say 1e8, (thus   truncating the unnecessary digits),
  27. >  and divide it by 1e8 to get back the original value.
  28.  
  29. >* After every few additions, say 1000, do the above operation.
  30.  
  31. >In  both the above operations,  a severe problem  would arise in cases
  32. >when  the  value represented  is less  than  the value  asked for. For
  33. >example, 
  34.  
  35. >   f= 213.22 would be represented as 213.2199999999999988631316228
  36.  
  37.    I would follow the approach, essentially, you mention above about doing
  38. a truncation operation every 1,000 or so additions.  However, I wouldn't
  39. convert to an unsigned long int to do so; I doubt the value early into your
  40. program could be contained in a long int, much less after multiplying by 1e8!
  41.  
  42. >* Is it  possible to set  to zero,  say the last  10-15  digits of the
  43. >  decimal part   without any effect  in the  long  run on  the 5 digit
  44. >  precision I require?
  45.  
  46.    In a set of tests I just ran on my machine (your mileage may vary), I
  47. had no problem with the use of the ceil() and floor() in effectively rounding
  48. doubles, and zeroing the late digits.  I'm not sure of what the standard
  49. guarantees for their precision.
  50.  
  51.    When epsilon is a power of 10, say 0.0001, for positive n you could try
  52. epsilon * floor( d * (1.0 / epsilon));  and that might be all you need.
  53.  
  54.                                                        -- Stone
  55. --
  56. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  57. # - Computer Science Undergrad, Montclair State University, NJ
  58. # - My views are my own, nobody else is this creative.  3;)>
  59. ... 2 + 2 = Ask My Calculator.
  60.